package com.movisol.messenger.views.controls;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.movisol.messenger.R;
import com.movisol.messenger.helper.MessengerHelper;
import com.movisol.messengerprofileservice.data.Friend;
import com.movisol.tools.HelperUtils;

public class ContactRow extends LinearLayout {
	
	private TextView nickNameTv;
	private TextView optionalTv;
	private ImageView imageContactRow;
	private Button contactButton;
	private Friend friend;
	private final Context context;
	private final float scale;
	private final int size;
	
	public ContactRow(Context context, Friend friend) {
		super(context);
		this.context = context;
		String infService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater li = (LayoutInflater)context.getSystemService(infService);
		li.inflate(R.layout.contactrow, this, true);
		
		this.friend = friend;
		scale = context.getResources().getDisplayMetrics().density;
		size = HelperUtils.dpToPixels(60, scale);
		initializeControls();
		
	}

	private void initializeControls() {
		
		nickNameTv = (TextView) findViewById(R.id.tvNickNameContactRow);
		optionalTv = (TextView) findViewById(R.id.tvOptionalContactRow);
		imageContactRow = (ImageView) findViewById(R.id.imageContactRow);
		contactButton = (Button) findViewById(R.id.imBtnContactRow);
		
		nickNameTv.setText(friend.getUserProfile().getCalculatedNickName());
		optionalTv.setText(friend.getUserProfile().getEmail());

		if(friend.getUserProfile().getPictureUrl() != null)
		{
			Runnable r = new Runnable() {
				
				@Override
				public void run() {
					Bitmap b = MessengerHelper.downloadBitmap(friend.getUserProfile().getPictureUrl());
					
					if(b != null)
					{
						Drawable d = new BitmapDrawable(context.getResources(),HelperUtils.getRoundedCornerBitmap(b,10));
						d.mutate();
						imageContactRow.setImageDrawable(d);
						
						imageContactRow.setLayoutParams(new LayoutParams(size, size));
					}
					
				}
			};

			Thread t = new Thread(r);
			t.run();
		}
	}


	@Override
	public void setSelected(boolean selected) {
		super.setSelected(selected);

		if(selected)
		{
			Drawable d = new ColorDrawable(Color.GRAY);
			d.setAlpha(128);
			setBackgroundDrawable(d);
		}
		else
		{
			setBackgroundDrawable(null);
		}
	}

	public Friend getFriend() {
		return friend;
	}

	public void setFriend(Friend friend) {
		this.friend = friend;
	}

	public Button getContactButton() {
		return contactButton;
	}

	public void setContactButton(Button contactButton) {
		this.contactButton = contactButton;
	}

	public ImageView getImageContactRow() {
		return imageContactRow;
	}

	public void setImageContactRow(ImageView imageContactRow) {
		this.imageContactRow = imageContactRow;
	}


	 

}
